package com.hellobike.haxing.web.withdrew;


import com.alibaba.fastjson.JSON;
import com.hellobike.haxing.common.ResponseCodeEnum;
import com.hellobike.haxing.order.result.Result;
import com.hellobike.haxing.order.result.ResultCode;
import com.hellobike.haxing.user.api.ShopWithdrewApi;
import com.hellobike.haxing.user.request.ShopWithdrewApplyInfoRequest;
import com.hellobike.haxing.user.request.ShopWithdrewApplyRequest;
import com.hellobike.haxing.user.response.BaseResponse;
import com.hellobike.haxing.user.response.ShopWithdrewApplyResponse;
import com.hellobike.haxing.user.response.ShopWithdrewConfigureResponse;
import com.hellobike.haxing.user.response.WithdrewSmsCodeResponse;
import com.hellobike.haxing.user.vo.SmallShopVo;
import com.hellobike.haxing.util.RedisUtil;
import com.hellobike.haxing.util.UserUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Api(tags = "提现接口")
@Slf4j
@RestController
@RequestMapping("/withdrew")
public class WithdrewController {
    @Reference(version = "1.0.0")
    private ShopWithdrewApi shopWithdrewApi;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private UserUtil userUtil;

    @ApiOperation("提现查询")
    @PostMapping(value = "/queryWithdrewApplyInfo")
    public Result<ShopWithdrewApplyResponse> queryWithdrewApplyInfo(@Valid @RequestBody ShopWithdrewApplyInfoRequest res) {
        // if (res.getAmount().compareTo(ShopWithdrewConfigureResponse.amount) < 0) {
        //     return Result.error(ResponseCodeEnum.NO_PARAM.code(), "提现额度小于规定最小提现额度");
        // }
        res.setShopId(userUtil.getUser().getShopVo().getId());
        ShopWithdrewApplyResponse response = shopWithdrewApi.queryWithdrewApplyInfo(res);
        if (!response.isSuccess()) {
            return Result.error(ResponseCodeEnum.NO_PARAM.code(), response.getMessage());
        }
        return Result.ok(response);
    }

    @ApiOperation("提现参数配置")
    @GetMapping(value = "/queryWithdrewConfigure")
    public Result<ShopWithdrewConfigureResponse> queryWithdrewConfigure() {
        ShopWithdrewConfigureResponse response = shopWithdrewApi.queryWithdrewConfigure(userUtil.getUser().getShopVo().getId());
        return Result.ok(response);
    }

    @ApiOperation("提现发送短信验证码")
    @GetMapping(value = "/sendWithdrewSmsCode")
    public Result<WithdrewSmsCodeResponse> sendWithdrewSmsCode() {
        WithdrewSmsCodeResponse response = shopWithdrewApi.sendWithdrewSmsCode(userUtil.getUser().getShopVo().getMobile());
        Result<WithdrewSmsCodeResponse> result = Result.ok(response);
        if (!response.isSuccess() && StringUtils.isBlank(response.getCode())) {
            result.faild(null, ResponseCodeEnum.SYSTEM_ERROR.code(), "短信已达上限，请明天再试");
        }
        return result;
    }

    @ApiOperation("确认提现申请")
    @PostMapping(value = "/confirmWithdrewApply")
    public Result<Boolean> confirmWithdrewApply(@Valid @RequestBody ShopWithdrewApplyRequest res) {
        log.info("=确认提现申请=参数=={}=", JSON.toJSONString(res));
        SmallShopVo smallShopVo = userUtil.getUser().getShopVo();
        try {
            res.setShopId(smallShopVo.getId());
            res.setMobile(smallShopVo.getMobile());
            BaseResponse<Boolean> response = shopWithdrewApi.confirmWithdrewApply(res);
            if (!response.isSuccess()) {
                return Result.error(false, ResultCode.FAIL, response.getMessage());
            }
        } catch (Exception e) {
            log.error("ddd", e);
            return Result.error(false, ResultCode.FAIL, e.getMessage());
        }
        return Result.ok(true, "提现申请成功");
    }
}
